package com.jiaertangshi.jiaer.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jiaertangshi.jiaer.dao.BaseAreaMapper;
import com.jiaertangshi.jiaer.dto.BaseArea;
import com.jiaertangshi.jiaer.dto.vo.City;
import com.jiaertangshi.jiaer.dto.vo.FirstWord;
import com.jiaertangshi.jiaer.tools.HanyuPinyinHelper;


@Service
public class BaseAreaService {
	
	@Autowired
	BaseAreaMapper  baseAreaMapper;
	
	
	public  List<BaseArea> getAllProvince( int parentid){
		return 	baseAreaMapper.getAllProvince(parentid);
	}


	public List<FirstWord> locationByFirstWord() {
		List<FirstWord>  fws=new  ArrayList<FirstWord>();
		List<BaseArea>       bas =   baseAreaMapper.getAll();
		List<BaseArea>       resBas = new  ArrayList<BaseArea>();
		for(int i=0;i<bas.size();i++){
			if(!bas.get(i).getCityname().equals("辖")){
				if(bas.get(i).getParentid()!=0){
					resBas.add(bas.get(i));	
				}
			}
		}
		BaseArea  ba1=new  BaseArea();
		ba1.setCityname("北京");
		ba1.setCodeid(11);
		resBas.add(ba1);
		BaseArea  ba2=new  BaseArea();
		ba2.setCityname("天津");
		ba2.setCodeid(12);
		resBas.add(ba2);
		BaseArea  ba3=new  BaseArea();
		ba3.setCityname("重庆");
		ba3.setCodeid(50);
		resBas.add(ba3);
		BaseArea  ba4=new  BaseArea();
		ba4.setCityname("上海");
		ba4.setCodeid(31);
		resBas.add(ba4);
		
		char [] array=new char[]{'A','B','C','D','E','F','G','H','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
		
		for(int   a=0;a<array.length;a++){
			FirstWord   firstWord=new FirstWord(); 
			firstWord.setLetter(array[a]+"");
			List<City>	tcitys=new  ArrayList<City>();
			
			for(int  p=0;p<resBas.size();p++){
				String		  upte = HanyuPinyinHelper.getFirstLetter(resBas.get(p).getCityname());
				if(upte.equals(array[a]+"")){
					City	inCity=new  City();
					inCity.setCityName(resBas.get(p).getCityname());
					inCity.setId(resBas.get(p).getCodeid()+"");
					tcitys.add(inCity);
				}
			}
			
			firstWord.setCitys(tcitys);
			fws.add(firstWord);
		}
 		
		return fws;
	}
	
	
	
	
}
